const api = require('../../../../ws_micro_shop/utils/api.js');
const tools = require('../../../../ws_micro_shop/utils/getuserinfo.js');
const app = getApp();

Page({
  data: {
    hide: 1,
    qrcode: '',
    theme: app.themeData,
    showpaytypes: false,
    unit: '',
    imgUrl: app.imgUrl,
  },
  onLoad(opts) {
    let that = this;
    this.setData({
      orgPrice: app.ws_micro_shop_globalData.orgPrice,
    })
    let options = opts || {};
    app.tabbarindex = options.tabbarindex ? options.tabbarindex - 0 : app.tabbarindex;
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    let userinfo = wx.getStorageSync('user_info');
    Number(userinfo.info) === 0 && tools.onGetInfo(this, userinfo.auth_mode);
    this.setData({
      opts: options,
      userinfo,
    });
    this.getOrderDetails(options);
  },
  getOrderDetails(e) {
    let oid = e.oid;
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.book.order_details,
      method: 'GET',
      data: {
        id: oid,
      },
      success: function (res) {
        if (res.code === 0) {
          let order_form = res.data.orderForm;
          for (let i in order_form) {
            if (Object.prototype.hasOwnProperty.call(order_form, i)) {
              let item = order_form[i];
              //判断类型是否为上传图片,将value字符串拆分为数组
              if (item.type === 'uploadimg') {
                let valueArr = item.value.split(',');
                res.data.orderForm[i].valueArr = valueArr;
              }
            }
          };
          that.setData({
            goods: res.data,
            pay_type_list: res.data.pay_type_list,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
            success: function (res) {
              wx.navigateBack();
            },
          });
        }
      },
      complete: function () {
        setTimeout(function () {
          wx.hideLoading();
        }, 1000);
      },
    });
    app.loadShopSetting().then(res => {
      that.setData(res);
    });
  },
  customerTo() {
    let { card_openid, card_toopenid, card_id } = this.data;
    let url = '/common/component/universal-card/pages/message?openid=' + card_openid + '&to_openid=' + card_toopenid + '&cardid=' + card_id + '&scene=inner&m_id=0';
    app.util.navigateTo(url);
  },
  goToGoodsDetails() {
    app.util.navigateTo('/ws_micro_shop_book/pages/book/details/details',{
      id: this.data.goods.goods_id,
    });
  },
  orderCancel(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否取消该预约订单',
      success: function (res) {
        if (res.cancel) {
          return true;
        }
        if (res.confirm) {
          wx.showLoading({
            title: '正在加载',
            mask: true,
          });
          app.ws_micro_shoprequest({
            url: api.book.order_cancel,
            data: { id },
            success: function (res) {
              if (res.code === 0) {
                app.util.navigateTo('/ws_micro_shop_book/pages/book/order/order', { status: 5 }, true);
              }
            },
            complete: function () {
              wx.hideLoading();
            },
          });
        }
      }
    })
  },
  orderPay(e) {
    let payment = Number(e.currentTarget.dataset.payment);
    let { orderID: id, userinfo } = this.data;
    wx.showLoading({
      title: '正在提交',
      mask: true,
    });
    this.setData({
      showpaytypes: false,
    });
    if (payment === 0) {
      this.WechatPay(id);
    } else if (payment === 3) {
      this.setData({
        showpaytypes: false,
      });
      this.BalancePay(id);
    } else if (payment === 5) {
      if (userinfo.unionid && userinfo.card) {
        this.CardPay(id);
      } else {
        wx.hideLoading();
        wx.showModal({
          title: '友情提示',
          content: '你还未开通会员卡，是否前往个人中心开通？',
          success: function (res) {
            if (res.confirm) {
              app.util.navigateTo('/ws_micro_shop/pages/user/user', {}, false);
            }
          },
        });
      }
    }
  },
  applyRefund(e) {
    let id = e.currentTarget.dataset.id;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.book.apply_refund,
      data: {
        order_id: id,
      },
      success: function (res) {
        if (res.code === 0) {
          wx.showModal({
            title: '提示',
            content: res.msg || '申请退款成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                app.util.navigateTo('/ws_micro_shop_book/pages/book/order/order', { status: 3 }, true);
              }
            },
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg || '申请退款失败',
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      },
    });
  },
  WechatPay(id) {
    app.ws_micro_shoprequest({
      url: api.book.order_pay,
      data: {
        id,
        pay_type: 'WECHAT_PAY',
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (res) {
        if (res.code === 0) {
          let { timeStamp, nonceStr, signType, paySign } = res.data;
          wx.requestPayment({
            timeStamp,
            nonceStr,
            package: res.data.package,
            signType,
            paySign,
            complete: function (e) {
              if (e.errMsg === 'requestPayment:fail' || e.errMsg === 'requestPayment:fail cancel') {
                wx.showModal({
                  title: '提示',
                  content: '订单尚未支付',
                  showCancel: false,
                  confirmText: '确认',
                  success: function (res) {
                    if (res.confirm) {
                      app.util.navigateTo('/ws_micro_shop_book/pages/book/order/order', { status: 0 }, true);
                    }
                  },
                });
                return;
              }
              app.util.navigateTo('/ws_micro_shop_book/pages/book/order/order', { status: 1 }, true);
            },
          });
        }
        if (res.code === 1) {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
          });
        }
      },
    });
  },
  BalancePay(id) {
    app.ws_micro_shoprequest({
      url: api.book.order_pay,
      data: {
        id,
        pay_type: 'BALANCE_PAY',
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (res) {
        if (res.code === 0) {
          app.util.navigateTo('/ws_micro_shop_book/pages/book/order/order', {
            status: 1,
          }, true);
        }
        if (res.code === 1) {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
          });
        }
      },
    });
  },
  CardPay(id) {
    app.ws_micro_shoprequest({
      url: api.book.order_pay,
      data: {
        id,
        pay_type: 'CARD_PAY',
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (res) {
        if (res.code === 0) {
          app.util.navigateTo('/ws_micro_shop_book/pages/book/order/order', {
            status: 1,
          }, true);
        }
        if (res.code === 1) {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
          });
        }
      },
    });
  },
  showPayType(e) {
    this.setData({
      showpaytypes: true,
      orderID: e.currentTarget.dataset.id,
    });
  },
  disabledClick() {
    // it`s necessary
  },
  hidePayType() {
    this.setData({
      showpaytypes: false,
    });
  },
  goToShopList() {
    wx.redirectTo({
      url: '/ws_micro_shop_book/pages/book/shop/shop?ids=' + this.data.goods.shop_id,
    });
  },
  orderQrcode() {
    let that = this;
    let goods = that.data.goods;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    if (goods.offline_qrcode) {
      that.setData({
        hide: 0,
        qrcode: goods.offline_qrcode,
      });
      wx.hideLoading();
    } else {
      app.ws_micro_shoprequest({
        url: api.book.get_qrcode,
        data: {
          order_no: goods.order_no,
        },
        success: function (res) {
          if (res.code === 0) {
            that.setData({
              hide: 0,
              qrcode: res.data.url,
            });
          } else {
            wx.showModal({
              title: '提示',
              content: res.msg,
            });
          }
        },
        complete: function () {
          wx.hideLoading();
        },
      });
    }
  },
  hide() {
    this.setData({
      hide: 1,
    });
  },
  comment(e) {
    wx.navigateTo({
      url: '/ws_micro_shop/pages/order-comment/order-comment?id=' + e.currentTarget.dataset.id + '&entry=book',
    });
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
  /*
   *预览表单上传的图片
   */
  previewUpImage(e) {
    let that = this;
    let url = e.currentTarget.dataset.url;
    let idx = e.currentTarget.dataset.index;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: that.data.goods.orderForm[idx].valueArr, // 需要预览的图片http链接列表
    });
  },
});
